//
//  LDAllClassViewController.m
//  dmao-customer
//
//  Created by macbook on 2017/10/16.
//  Copyright © 2017年 秦慕乔. All rights reserved.
//

#import "LDAllClassViewController.h"
#import "HeaderReusableView.h"
#import "LDAllClassCollectionViewCell.h"
#import "LDHomeNextViewController.h"

@interface LDAllClassViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

@implementation LDAllClassViewController
{
    UICollectionView *_collectionView;
    NSMutableArray *_detailIdArray;
    NSMutableArray *_nameArray;
    NSMutableArray *_sectionArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"全部分类";
    _detailIdArray = [NSMutableArray array];
    _nameArray = [NSMutableArray array];
    _sectionArr = [NSMutableArray array];
    self.view.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [self downloadData];
    [self createCollectionView];
}

- (void)downloadData
{
    [THWManager GetHttp:API(LDAllClass) Parameters:nil Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([responseObject[@"data"] count] != 0) {
            for (NSDictionary *dic in responseObject[@"data"]) {
                [_detailIdArray addObject:dic[@"class_id"]];
                [_nameArray addObject:dic[@"class_name"]];
                [_sectionArr addObject:dic[@"list"]];
            }
        }
        
        [_collectionView reloadData];
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

#pragma mark- collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 70, WIDTH, HEIGHT-70) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [_collectionView registerNib:[UINib nibWithNibName:@"LDAllClassCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"LDAllClassCollectionViewCell"];
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"HeaderReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    [self.view addSubview:_collectionView];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _nameArray.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableView = nil;
    UILabel *nameLabel = nil;
    
    if (kind == UICollectionElementKindSectionHeader) {
        reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        if (![reusableView viewWithTag:66]) {
            nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 50)];
            nameLabel.font = [UIFont systemFontOfSize:16];
            nameLabel.textColor = [UIColor colorWithHex:0x333333];
            [reusableView addSubview:nameLabel];
            nameLabel.tag = 66;
        }else{
            nameLabel = [reusableView viewWithTag:66];
        }
        
    }
    nameLabel.text = _nameArray[indexPath.section];
    return reusableView;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGSize size={WIDTH,45};
    return size;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *arr = _sectionArr[section];
    return arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LDAllClassCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LDAllClassCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    NSArray *arr = _sectionArr[indexPath.section];
    cell.TypeLabel.text = [arr[indexPath.row] objectForKey:@"catName"];
    cell.TypeLabel.textColor = [UIColor colorWithHex:0x999999];
    cell.TypeLabel.font = [UIFont systemFontOfSize:13.0f];
    cell.TypeLabel.layer.borderColor = [[UIColor colorWithHex:0x999999]CGColor];
    cell.TypeLabel.layer.borderWidth = 1.0f;
    cell.TypeLabel.layer.masksToBounds = YES;
    return cell;
}

// 设置cell的水平间距
- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

// 设置cell的竖直间距
- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(70, 30);
}

//定义每个UICollectionView 的 margin(边缘，范围)
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    // 上左下右的空隙
    if (IPHONE5S) {
        return UIEdgeInsetsMake(0, 30, 0, 30);
    }else{
        return UIEdgeInsetsMake(0, 20, 0, 20);
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LDHomeNextViewController *next = [[LDHomeNextViewController alloc] init];
    next.classId = _detailIdArray[indexPath.section];
    next.titleStr = _nameArray[indexPath.section];
    [self.navigationController pushViewController:next animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
